package it.eng.area118.mdogpscollector.model;

import it.eng.area118.mdogpscollector.db.Item;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.garret.perst.IPersistentList;
import org.garret.perst.Persistent;

public class CheckPoint extends Persistent implements Serializable, Item {

	/* id del punto in co se vuoto l'utente l'ha inserito ex-novo */
	private String coId;

	/*
	 * descrizione estesa del punto ad es: "AUTOSTRADA A4 KM 1" precaricata o inseribile dall'utente
	 */

	private String desc;

	/* tipo punto AD ES AUTOSTRADA */
	private String type;

	private String name;

	/* human readable direction */
	private String direction;

	/* DESCRIZIONE DEL PUNTO AD ES: KM14 */
	private String point;

	/* NOTE DEL PUNTO AD ES COINCIDENZA CON CAVALCAVIA, PONTE ETC */
	private String pointNote;

	/* COORDINATE RILEVATE PER IL checkpoint */
	private GpsLocation location;

	/* LISTA DEI MARKERS APPARTENENTI AL CHECK POINT */
	private IPersistentList markers;

	// private String key;

	public String getCoId() {
		return coId;
	}

	public void setCoId(String coId) {
		this.coId = coId;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getPoint() {
		return point;
	}

	public void setPoint(String point) {
		this.point = point;
	}

	public String getPointNote() {
		return pointNote;
	}

	public void setPointNote(String pointNote) {
		this.pointNote = pointNote;
	}

	public GpsLocation getLocation() {
		return location;
	}

	public void setLocation(GpsLocation location) {
		this.location = location;
	}

	public List getMarkers() {
		return markers;
	}

	public void setMarkers(IPersistentList markers) {
		this.markers = markers;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	// @Override
	// public String toString() {
	// return "CheckPoint [coId=" + coId + ", desc=" + desc + ", type=" + type
	// + ", name=" + name + ", direction=" + direction + ", point="
	// + point + ", pointNote=" + pointNote + ", location=" + location
	// + ", markers=" + markers + "]";
	// }o

	@Override
	public String toString() {
		String s = "";
		s += type + " " + name + " " + "KM " + point + " " + direction;
		return s;
	}

	@Override
	public String getItemKey() {
		return name + "-" + point + "-" + direction;
	}

	// public void setKey(String key) {
	// this.key = key;
	// }

	public String getCoordDetail() {
		if (location != null) {
			return stringifyLocation();
		} else {
			return "NO COORD";
		}

	}

	private String stringifyLocation() {
		StringBuffer buffer = new StringBuffer();
		if (location != null) {
			DateFormat format = SimpleDateFormat.getDateTimeInstance();
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(location.getTimestamp());
			buffer.append(format.format(c.getTime()));
			buffer.append("\n");
			buffer.append("Latitudine: " + location.getLatitude());
			buffer.append("\n");
			buffer.append("Longitudine: " + location.getLongitude());
			buffer.append("\n");
			buffer.append("Accuratezza: " + location.getAccuracy());
			buffer.append("\n");
			buffer.append("Numero Satelliti: " + location.getSatNumber());
		}
		return buffer.toString();
	}

}
